package com.youmans.userservice.shiro.realms;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.youmans.userservice.entity.YoumansUser;
import com.youmans.userservice.service.YoumansUserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;

public class CustomerRealm extends AuthorizingRealm {
    @Autowired
    YoumansUserService userService ;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        //获取身份信息
//        String primaryPrincipal = (String) principals.getPrimaryPrincipal();
//        System.out.println("调用授权验证"+primaryPrincipal);
////        根据主身份信息获取角色和权限信息
//        UserService userService = (UserService) ApplicationContextUtils.getBean("userService");
//        User user = userService.findRolesByUserName(primaryPrincipal);
//        //授权角色信息
//        if(!CollectionUtils.isEmpty(user.getRoles())){
//            SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//            user.getRoles().forEach(role -> {
//                simpleAuthorizationInfo.addRole(role.getName());
//                // 权限信息
//                List<Perms> perms = userService.findPermsByRoleId(role.getId());
//                if(!CollectionUtils.isEmpty(perms)){
//                    perms.forEach(perm ->{
//                        simpleAuthorizationInfo.addStringPermission(perm.getName());
//                    } );
//                }
//            });
//            return simpleAuthorizationInfo ;
//        }
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.err.println("==================");
//        //根据身份信息
        String principal = (String) token.getPrincipal();
////        在工厂中获取service对象
//        YoumansUserService userService = (YoumansUserService) ApplicationContextUtils.getBean("youmansUserService");
        YoumansUser user = userService.selectInfoByName(principal);

        if(!ObjectUtils.isEmpty(user)){
//            return new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(),new MyByteSource(user.getSalt()),this.getName()) ;
            System.err.println(this.getName());
            return new SimpleAuthenticationInfo(user.getUsername(),user.getPassword(),
                    ByteSource.Util.bytes(user.getSalt()),this.getName());
        }
        return null;
    }
}
